public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length;
        int n = matrix[0].length;
        if(m == 0)
            return false;
        int first = 0;
        int last = m * n;

        while(first < last) {
            int mid = first + (last - first) / 2;
            int value = matrix[mid / n][mid % n];

            if(value == target)
                return true;
            else if(value < target)
                first = mid + 1;
            else
                last = mid;
        }
        return false;
    }
}